home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Utilities / Winter Shell 1.0d2 / Source / Libraries / WindowMenuLib / WindowMenuLib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-20  |  3.7 KB  |  136 lines  |  [TEXT/KAHL]

  1. /*    Functions for maintaining a menu containing a list of windows. Selecting
  2.     an item from the menu brings the window with that title to the front.
  3.     
  4.     94/01/06 aih
  5.     - an array is used to associate windows with menu items, instead of
  6.     relying only on the menu item's title, which used to cause conflicts
  7.     if more than one window used the same title
  8.     
  9.     93/03/06 AIH
  10.     - The current window has a check-mark placed next to its name
  11.     
  12.     92/02/22 AIH
  13.     - Improved event handling
  14.     
  15.     91/06/13 AIH
  16.     - Inserting a menu item with an empty title is ignored, so InsMenuItem
  17.     is called with the title string just before calling SetItem
  18.     
  19.     91/05/31 AIH
  20.     - SetItem is used to avoid interpreting characters in a window's title
  21.     as menu metacharacters
  22.     
  23.     91/05/20 AIH
  24.     - Trying to get the window insertion to work correctly
  25.     
  26.     91/05/14 Ari Halberstadt (AIH)
  27.     - Created this library. */
  28.  
  29. #include <string.h>
  30. #include <Packages.h>
  31. #include "ArrayListLib.h"
  32. #include "EventLib.h"
  33. #include "MenuLib.h"
  34. #include "ResourceConstantsLib.h"
  35. #include "WindowLib.h"
  36. #include "WindowMenuLib.h"
  37.  
  38. /* The first item in the windows menu containing a window title. You
  39.     could change this so that the first few items would do various
  40.     window actions, such as Tile, Clean Up, Close All, etc. */
  41. #define FIRST_WINDOW_ITEM (1)
  42.  
  43. static ArrayListHandle gWindowMenuArray;
  44.  
  45. /* return menu item containing window, or zero if not found */
  46. static short WinMenuFind(WindowPtr find)
  47. {
  48.     WindowPtr window = NULL;
  49.     short i = 0, n;
  50.     
  51.     if (find && gWindowMenuArray) {
  52.         n = ArrayListCount(gWindowMenuArray);
  53.         for (i = 0; i < n; i++) {
  54.             ArrayListGet(gWindowMenuArray, i, &window);
  55.             if (window == find) {
  56.                 i += FIRST_WINDOW_ITEM;
  57.                 break;
  58.             }
  59.         }
  60.     }
  61.     return(window == find ? i : 0);
  62. }
  63.  
  64. /* add the title of the window to the windows menu */
  65. void WinMenuAdd(WindowPtr window)
  66. {
  67.     Str255 title;        /* title of window */
  68.     Str255 item;        /* text of current menu item */
  69.     short nitems;        /* number of items in menu */
  70.     short i;                /* index to current menu item */
  71.     MenuHandle menu;    /* the windows menu */
  72.  
  73.     require(WinValid(window));
  74.     require(WinLayer(window) == WIN_LAYER_DOCUMENT);
  75.     if (! WinMenuFind(window)) {
  76.         /* insert window's title into menu (in sorted order) */
  77.         GetWTitle(window, title);
  78.         menu = MenuCmdHandle(CMD_WINDOW);
  79.         nitems = CountMItems(menu);
  80.         for (i = 1; i <= nitems; i++) {
  81.             GetItem(menu, i, item);
  82.             if (IUCompString(title, item) <= 0)
  83.                 break;
  84.         }
  85.         InsMenuItem(menu, (StringPtr) "\pany non-empty title", i - 1);
  86.         SetItem(menu, i, title);
  87.         /* insert window into array */
  88.         if (! gWindowMenuArray)
  89.             gWindowMenuArray = ArrayListBegin(sizeof(WindowPtr));
  90.         ArrayListInsert(gWindowMenuArray, i - FIRST_WINDOW_ITEM);
  91.         ArrayListSet(gWindowMenuArray, i - FIRST_WINDOW_ITEM, &window);
  92.     }
  93.     ensure(WinMenuFind(window) > 0);
  94. }
  95.  
  96. /* remove the window from the windows menu */
  97. void WinMenuRemove(WindowPtr window)
  98. {
  99.     short item;
  100.     
  101.     require(WinValid(window));
  102.     require(WinLayer(window) == WIN_LAYER_DOCUMENT);
  103.     item = WinMenuFind(window);
  104.     if (item) {
  105.         DelMenuItem(MenuCmdHandle(CMD_WINDOW), item);
  106.         ArrayListDelete(gWindowMenuArray, item - 1);
  107.     }
  108. }
  109.  
  110. /* adjust the windows menu */
  111. void WinMenuAdjustMenu(void)
  112. {
  113.     MenuHandle menu;
  114.     
  115.     menu = MenuCmdHandle(CMD_WINDOW);
  116.     if (! WinModalHasFocus() && CountMItems(menu) > 0) {
  117.         MenuCmdEnable(CMD_WINDOW);
  118.         CheckItem(menu, WinMenuFind(WinFirstVisible(WIN_LAYER_DOCUMENT)), true);
  119.     }
  120. }
  121.  
  122. /* handle a selection from the windows menu by selecting the
  123.     corresponding window */
  124. Boolean WinMenuDoMenu(const MenuPickType *pick)
  125. {
  126.     Boolean handled = false;
  127.     WindowPtr window;
  128.     
  129.     if (pick->cmd == CMD_WINDOW) {
  130.         ArrayListGet(gWindowMenuArray, pick->item - FIRST_WINDOW_ITEM, &window);
  131.         WinSelect(window);
  132.         handled = true;
  133.     }
  134.     return(handled);
  135. }
  136.